package com.itheima.serviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceimpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User FindUserByName(String Name) {
        User user = userDao.FindUserByName(Name);
        return user;
    }

    @Override
    public Set<Role> FindRoleById(Integer id) {
        Set<Role> roles = userDao.FindRoleById(id);
        return roles;
    }

    @Override
    public Set<Permission> FindPremissionById(Integer id) {
        Set<Permission> permissions = userDao.FindPremissionById(id);
        return permissions;
    }

    //修改密码
    @Override
    public boolean updatepassword(String oldcode, String newcode, String username) {
        //查询该用户当前密码
       String password = userDao.querypassword(username);
       //判断用户当前密码与前端传过来的旧密码是否相同
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(oldcode, password);
        if(matches){
//            相同 ; 调用修改密码方法进行修改密码
            //将新密码进行加密
            String encode = bCryptPasswordEncoder.encode(newcode);
            //将用户名与新密码进行封装
            Map<String,String> map = new HashMap<>();
            map.put("username",username);
            map.put("newcode",encode);
            //修改密码,
            userDao.updatepassword(map);
            return true;
        }else {
//            不同 ； 旧密码输入错误
            return false;
        }

    }

    //验证该用户的密码是否正确
    @Override
    public boolean Ackpassword(Map user) {
        String username = (String)user.get("username");  //前端传过来的用户名
        String _password = (String)user.get("password");//前端传过来的密码
        String password = userDao.querypassword(username); //后台保存的密码
        //判断用户当前密码与前端传过来的旧密码是否相同
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        boolean matches = bCryptPasswordEncoder.matches(_password, password);
        return matches;
    }

    //下面是关于用户表操作的
    @Override
    public void add(User user) {
        String password = user.getPassword();
        if (password==null){
            //user.setPassword("12356");
            String encode = new BCryptPasswordEncoder().encode("123456");
            user.setPassword(encode);
        }else {
            String encode = new BCryptPasswordEncoder().encode(user.getPassword());
            user.setPassword(encode);
        }
        userDao.add(user);
    }

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) throws Exception {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);//使用分页助手完成
        Page<User> page= userDao.selectByCondition(queryString);
        List<User> result = page.getResult();
        for (User user : result) {
            Date birthday1 = user.getBirthday();
            String s = DateUtils.parseDate2String(birthday1);
            user.setBirthday1(s);
        }
        return new PageResult(page.getTotal(), result);
    }

    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    @Override
    public List<Role> findAll() {
        return userDao.findAll();
    }

    @Override
    public void edit(User user, Integer[] roles) {
        Integer userId = user.getId();
        userDao.deleteAssociation(userId);
        this.setUserAndRole(userId,roles);
    }

    @Override
    public void DeleteById(Integer id) {

        //清除用户与角色之间的关联
        userDao.delete1(id);
        //删除用户
        userDao.DeleteById(id);


//        Integer UserCount= userDao.queryRoleById(id);
//        if(UserCount<=0){
//            //该用户没有和角色绑定
//            userDao.DeleteById(id);
//            return true;
//        }else {
//            //该用户已和角色绑定
//            return false;
//        }
    }

    public  void setUserAndRole(Integer userId,Integer[] roles){
        if (roles!=null&&roles.length>0){
            for (Integer role:roles){
                Map<String,Integer> map=new HashMap<>();
                map.put("userId",userId);
                map.put("role",role);
                userDao.setUserAndRole(map);
            }
        }
    }
}
